package org.hongbo.springbootebsapi.controller;

import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.hongbo.springbootebsapi.annotations.ResponseResult;
import org.hongbo.springbootebsapi.enums.ResultCode;
import org.hongbo.springbootebsapi.exceptions.BusinessException;
import org.hongbo.springbootebsapi.jwt.Audience;
import org.hongbo.springbootebsapi.jwt.JwtTokenUtil;
import org.hongbo.springbootebsapi.service.login.ILoginService;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@Api(value = "/Login", description = "登录的相关API")
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @Autowired
    private Audience audience;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    UserDetailsService userDetailsService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    StringEncryptor stringEncryptor;


    @ApiOperation(value="登录验证")
    @PostMapping("/loginVerify")
    public boolean loginVerify(@ApiParam(value = "用户名称",required = true) @RequestParam(value = "username") String username,
                              @ApiParam(value = "用户密码",required = true) @RequestParam(value = "password") String password){
        boolean verify = loginService.verifyLogin(username,password);

        if (verify) {
            return true;
        } else {
            return false;
        }
    }

    @ApiOperation(value="登录验证", notes="判断用户是否登录成功")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    })
    @PostMapping("/signVerify")
    @ResponseResult
    public String signVerify(@RequestParam(value = "username") String username,
                              @RequestParam(value = "password") String password){
        password = stringEncryptor.decrypt(password);

        String tokenStr = null;

        username = username.toUpperCase().trim();

        boolean verify = loginService.verifyLogin(username,password);

        if (verify){
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            tokenStr =audience.getTokenHead() +  jwtTokenUtil.generateToken(userDetails);
        }else{
            throw new BusinessException(ResultCode.USER_LOGIN_ERROR);
        }

        return tokenStr;
    }
}
